﻿package org.koyabr.webx.demo.danmaku.dal.dataobjs;

import java.text.DateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.koyabr.webx.demo.danmaku.dal.dao.MulToMulDao;
import org.koyabr.webx.demo.danmaku.dal.dao.PostDao;
import org.koyabr.webx.demo.danmaku.dal.dao.UserDao;

/**
 * 
 * 定义投稿的文章
 * 
 */
public class Post {
	/**
	 * 唯一标识
	 */
	private String id;
	/**
	 * 投稿日期
	 */
	private final Date date;
	/**
	 * 投稿者
	 */
	private final User author;

	/**
	 * 文章标题
	 */
	private String title;
	/**
	 * 文章类别
	 */
	private String category;
	/**
	 * 新浪视频vid
	 */
	private String vid;
	/**
	 * 缩略图URL（如果有）
	 */
	private String pictureUrl;
	/**
	 * 缩略图文件地址（如果有）
	 */
	private String pictureFile;
	/**
	 * 视频描述（如果有）
	 */
	private String description;
	/**
	 * 浏览/点击量
	 */
	private int playNum;

	@Autowired
	private MulToMulDao mulDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private PostDao postDao;

	public WatchSet watchSet = new WatchSet();
	public LikeSet likeSet = new LikeSet();
	public FavorSet favorSet = new FavorSet();

	/**
	 * 构造函数 自动计算投稿日期
	 * 
	 * @param author
	 *            投稿者
	 */
	public Post(User author) {
		this.author = author;
		this.date = new Date();
		this.id = "";
	}

	public String getId() {
		return id;
	}

	/**
	 * 
	 * 输出格式化的时间
	 */
	public String getDate() {
		return DateFormat.getDateTimeInstance(DateFormat.SHORT,
				DateFormat.SHORT).format(date);
	}

	public User getAuthor() {
		return author;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
		this.id = title;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getVid() {
		return vid;
	}

	public void setVid(String vid) {
		this.vid = vid;
	}

	public String getPictureUrl() {
		return pictureUrl;
	}

	public void setPictureUrl(String pictureUrl) {
		this.pictureUrl = pictureUrl;
	}

	public String getPictureFile() {
		return pictureFile;
	}

	public void setPictureFile(String pictureFile) {
		this.pictureFile = pictureFile;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public long getPlayNum() {
		return playNum;
	}

	public void setPlayNum(int playNum) {
		this.playNum = playNum;
	}

	// 一些对应关系

	/**
	 * 正在浏览此文章的用户
	 */
	public class WatchSet implements MulToMul<User> {
		public int size() {
			return toList().size();
		}

		public List<User> toList() {
			LinkedList<User> userList = new LinkedList<User>();

			for (Map<String, String> map : mulDao.getAllWatchUser(id)) {
				User user = userDao.getUserByName(map.get("username"));
				if (user != null) {
					userList.add(user);
				}
			}

			return userList;
		}

		public boolean contains(User user) {
			return mulDao.getWatch(user.getUsername(), id) != null;
		}

		public void add(User user) {
			mulDao.insertWatch(user.getUsername(), id);
		}

		public void remove(User user) {
			mulDao.deleteWatch(user.getUsername(), id);
		}
	}

	/**
	 * 喜欢此文章的用户
	 */
	public class LikeSet implements MulToMul<User> {
		public int size() {
			return toList().size();
		}

		public List<User> toList() {
			LinkedList<User> userList = new LinkedList<User>();

			for (Map<String, String> map : mulDao.getAllLikeUser(id)) {
				User user = userDao.getUserByName(map.get("username"));
				if (user != null) {
					userList.add(user);
				}
			}

			return userList;
		}

		public boolean contains(User user) {
			return mulDao.getLike(user.getUsername(), id) != null;
		}

		public void add(User user) {
			mulDao.insertLike(user.getUsername(), id);
		}

		public void remove(User user) {
			mulDao.deleteLike(user.getUsername(), id);
		}
	}

	/**
	 * 收藏此文章的用户
	 */
	public class FavorSet implements MulToMul<User> {
		public int size() {
			return toList().size();
		}

		public List<User> toList() {
			LinkedList<User> userList = new LinkedList<User>();

			for (Map<String, String> map : mulDao.getAllFavorUser(id)) {
				User user = userDao.getUserByName(map.get("username"));
				if (user != null) {
					userList.add(user);
				}
			}

			return userList;
		}

		public boolean contains(User user) {
			return mulDao.getFavor(user.getUsername(), id) != null;
		}

		public void add(User user) {
			mulDao.insertFavor(user.getUsername(), id);
		}

		public void remove(User user) {
			mulDao.deleteFavor(user.getUsername(), id);
		}
	}

}
